﻿
window.onload = InitializeMap;
var bMarkPoint = 0;
var map;

function InitializeMap() {
    //var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

    // Try HTML5 geolocation
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            var pos = new google.maps.LatLng(position.coords.latitude,
                                             position.coords.longitude);

            //                var infowindow = new google.maps.InfoWindow({
            //                    map: map,
            //                    position: pos,
            //                    content: 'Location found using HTML5.'
            //                });

            map.setCenter(pos);
        }, function () {
            handleNoGeolocation(true);
        });
    } else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
    }

    //creating drawing
//    var drawingManager = new google.maps.drawing.DrawingManager({
//        drawingMode: google.maps.drawing.OverlayType.MARKER,
//        drawingControl: true,
//        drawingControlOptions: {
//            position: google.maps.ControlPosition.TOP_CENTER,
//            drawingModes: [
//                    google.maps.drawing.OverlayType.MARKER,
//                    google.maps.drawing.OverlayType.CIRCLE
//                ]
//        },
//        markerOptions: {
//            icon: 'Images/marker.png'
//        },
//        circleOptions: {
//            fillColor: '#ffff00',
//            fillOpacity: 1,
//            strokeWeight: 5,
//            clickable: false,
//            editable: true,
//            zIndex: 1
//        }
//    });
//    drawingManager.setMap(map);

    //event
//    google.maps.event.addListener(drawingManager, 'overlaycomplete', function (event) {
//        if (event.type == google.maps.drawing.OverlayType.MARKER) {
//            //        var radius = event.overlay.getRadius();
//            alert('Complete marker');
//        }
//    });

    google.maps.event.addListener(map, 'click', function (e) {
        if (bMarkPoint == 1) {
            placeMarker(e.latLng, map);
        }
    });


    var input = document.getElementById('searchTextField');
    var autocomplete = new google.maps.places.Autocomplete(input);

    autocomplete.bindTo('bounds', map);

    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
        map: map
    });

    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        infowindow.close();
        var place = autocomplete.getPlace();
        if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
        } else {
            map.setCenter(place.geometry.location);
            map.setZoom(15);  // Why 17? Because it looks good.
        }

        var image = new google.maps.MarkerImage(
              place.icon,
              new google.maps.Size(71, 71),
              new google.maps.Point(0, 0),
              new google.maps.Point(17, 34),
              new google.maps.Size(35, 35));
        marker.setIcon(image);
        marker.setPosition(place.geometry.location);

        var address = '';
        if (place.address_components) {
            address = [(place.address_components[0] &&
                        place.address_components[0].short_name || ''),
                       (place.address_components[1] &&
                        place.address_components[1].short_name || ''),
                       (place.address_components[2] &&
                        place.address_components[2].short_name || '')
                      ].join(' ');
        }

        infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
        infowindow.open(map, marker);
    });

}

function placeMarker(position, map) {
    var marker = new google.maps.Marker({
        position: position,
        map: map
    });
    map.panTo(position);
    setMarkPointValue(0);

    var TextBoxX = document.getElementById(GetClientId("txtHidDataX"));
    TextBoxX.value = position.lat();
    var TextBoxY = document.getElementById(GetClientId("txtHidDataY"));
    TextBoxY.value = position.lng();

//    PageMethods.RemovePlace();
    var str = '<%=RemovePlace()%>';
    //alert(PageMethods.MethodA());
    //GetValue();
    //CallBack(position.lat(), position.lng());
}

function GetValue() {
    CallBack();
}
function GetName(arg, context) {
    //document.getElementById('txtPercentage').value = arg;
    alert(arg);
}

google.maps.event.addDomListener(window, 'load', initialize);

// set, get Mark Point
function setMarkPointValue(value) {
    bMarkPoint = value;
}

function getMarkPointValue() {
    //alert(window.myValue); // yup, it's "test"
}

// ============================simpleMarker================================== //
function simpleMarker() {
    var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
    var myOptions = {
        zoom: 4,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: "Hello World!"
    });
}

function PlaceViewMarker(lat, lng) {
    alert(lat);
    var myLatlng = new google.maps.LatLng(lat, lng);
    var myOptions = {
        zoom: 15,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    placeMarker(myLatlng, map);
}

function insideJS() {
    alert('Test alert');
}

function removePlace(idPlace) {
    var id2Remove = document.getElementById('idPlaceCurrent').value;
    if (myMarkers) {
        for (var i = 0; i < myMarkers.length; i++) {
            if (arrPlace[i].id == id2Remove)
                myMarkers[i].setMap(null);
               // var str = '<%=RemovePlace()%>';
        }
    }
}

/**
* The MarkerManager object.
* @type {MarkerManager}
*/
var mgr = null;

/**
* Toggles Marker Manager visibility.
*/
var showMarketManager = false;

function addMyPlace(id, lat, lng, sTitle, typeid) {
    //alert(id);
    //alert(lng);

    var contentString = '<div id="content">' +
        '<div id="siteNotice">' +
        '</div>' +
        '<h1 id="firstHeading" class="firstHeading">' + sTitle + '</h1>' +
        '<div id="bodyContent">' + sTitle +
        '<p><table><tr><td><a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">Edit</td>' +
        '<input type="hidden" id="idPlaceCurrent" value="'+id+'"/>' +
        '<td><a href="javascript:removePlace()">Delete</td></tr></table>' +
        '</div>' +
        '</div>';

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    var myLatlng = new google.maps.LatLng(lng, lat);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: sTitle,
        clickable: false,
        draggable: true,
        flat: true,
        zindex: typeid
    });

    google.maps.event.addListener(marker, 'click', function () {
        infowindow.open(map, marker);
    });

    //mgr.addMarker(marker, 1, 15);
   myMarkers.push(marker);   
}

// Sets the map on all markers in the array.
function setAllMap(map) {
    for (var i = 0; i < myMarkers.length; i++) {
        myMarkers[i].setMap(map);
    }
}

// Shows any overlays currently in the array.
function showOverlays() {
    setAllMap(map);
}

function showOverlaysParam(idType) {

    for (var i = 0; i < myMarkers.length; i++) {
        if (myMarkers[i].zindex == idType || idType == 0)
            myMarkers[i].setMap(map);
    }
}

function addAllPlaces() {
    if (arrPlace) {
        for (var i = 0; i < arrPlace.length; i++) {
            addMyPlace(arrPlace[i].id, arrPlace[i].lat, arrPlace[i].lng, arrPlace[i].title, arrPlace[i].typeid);
        }
    }
}

function changePlaceType(ddlId) {
    var idType = ddlId.options[ddlId.selectedIndex].value;
    setAllMap(null);
    showOverlaysParam(idType);
}   


